**Purpose: This file runs the main analysis for "Latinos' Fears of Deportation by Citizenship, 2007-2018." 

use "pew_data_allyrs.dta", clear

/// installing programs needed for analysis///
*ssc install estout
*ssc install outreg
*ssc install mdesc

///set scheme
set scheme plotplain

///Multiple Imputation
*flag variables for missing data on deportation worries is worry_missing

gen citizens_missing = 0
replace citizens_missing = 1 if citizen==.
tab citizens_missing

gen legal_missing = 0
replace legal_missing = 1 if legal_status==.
tab legal_missing

gen worry_missing = 0
replace worry_missing = 1 if worry2==.
tab worry_missing

save "pew_data_allyrs.dta", replace

*mi using mvn for citizenship indicator

mi set mlong
set seed 94305
mi register imputed worry2 citizen age educ pparty registeredvoter hh_adults
mi register regular sex census_region_res svy_year
mi impute chained (logit) worry2 (mlogit) citizen pparty registeredvoter (ologit) educ /// 
(regress) age hh_adults = sex i.census_region_res, replace add(10) augment

mi xeq 0: drop if citizens_missing==1
*mi xeq 0: drop if legal_missing==1
mi xeq 0: drop if worry_missing==1

global controls  age i.sex i.educ i.pparty hh_adults i.census_region_res i.svy_year
mi svyset id [pweight = svy_weight2] 

save "pew_data_allyrs-imputed-citizen.dta", replace

///SUMMARY STATISTICS

mi estimate: svy: proportion worry2
mi estimate: svy: proportion citizen
mi estimate: svy: proportion legal_status
mi estimate: svy: mean age
mi estimate: svy: mean sex
mi estimate: svy: proportion educ
mi estimate: svy: proportion pparty
mi estimate: svy: mean hh_adults
mi estimate: svy: proportion census_region_res
mi estimate: svy: proportion svy_year 

mi estimate: svy: proportion worry2 if svy_year==2007
mi estimate: svy: proportion citizen if svy_year==2007
mi estimate: svy: proportion legal_status if svy_year==2007
mi estimate: svy: mean age if svy_year==2007
mi estimate: svy: mean sex if svy_year==2007
mi estimate: svy: proportion educ if svy_year==2007
mi estimate: svy: proportion pparty if svy_year==2007
mi estimate: svy: mean hh_adults if svy_year==2007
mi estimate: svy: proportion census_region_res if svy_year==2007

mi estimate: svy: proportion worry2 if svy_year==2008
mi estimate: svy: proportion citizen if svy_year==2008
mi estimate: svy: proportion legal_status if svy_year==2008
mi estimate: svy: mean age if svy_year==2008
mi estimate: svy: mean sex if svy_year==2008
mi estimate: svy: proportion educ if svy_year==2008
mi estimate: svy: proportion pparty if svy_year==2008
mi estimate: svy: mean hh_adults if svy_year==2008
mi estimate: svy: proportion census_region_res if svy_year==2008

mi estimate: svy: proportion worry2 if svy_year==2010
mi estimate: svy: proportion citizen if svy_year==2010
mi estimate: svy: proportion legal_status if svy_year==2010
mi estimate: svy: mean age if svy_year==2010
mi estimate: svy: mean sex if svy_year==2010
mi estimate: svy: proportion educ if svy_year==2010
mi estimate: svy: proportion pparty if svy_year==2010
mi estimate: svy: mean hh_adults if svy_year==2010
mi estimate: svy: proportion census_region_res if svy_year==2010

mi estimate: svy: proportion worry2 if svy_year==2013
mi estimate: svy: proportion citizen if svy_year==2013
mi estimate: svy: proportion legal_status if svy_year==2013
mi estimate: svy: mean age if svy_year==2013
mi estimate: svy: mean sex if svy_year==2013
mi estimate: svy: proportion educ if svy_year==2013
mi estimate: svy: proportion pparty if svy_year==2013
mi estimate: svy: mean hh_adults if svy_year==2013
mi estimate: svy: proportion census_region_res if svy_year==2013

mi estimate: svy: proportion worry2 if svy_year==2016
mi estimate: svy: proportion citizen if svy_year==2016
mi estimate: svy: proportion legal_status if svy_year==2016
mi estimate: svy: mean age if svy_year==2016
mi estimate: svy: mean sex if svy_year==2016
mi estimate: svy: proportion educ if svy_year==2016
mi estimate: svy: proportion pparty if svy_year==2016
mi estimate: svy: mean hh_adults if svy_year==2016
mi estimate: svy: proportion census_region_res if svy_year==2016

mi estimate: svy: proportion worry2 if svy_year==2018
mi estimate: svy: proportion citizen if svy_year==2018
mi estimate: svy: proportion legal_status if svy_year==2018
mi estimate: svy: mean age if svy_year==2018
mi estimate: svy: mean sex if svy_year==2018
mi estimate: svy: proportion educ if svy_year==2018
mi estimate: svy: proportion pparty if svy_year==2018
mi estimate: svy: mean hh_adults if svy_year==2018
mi estimate: svy: proportion census_region_res if svy_year==2018

mi estimate: svy: logistic worry2 i.svy_year
mimrgns i.svy_year if worry2, predict(pr) cmdmargins
mimrgns i.svy_year if worry2, predict(pr) cmdmargins pwcompare
mi estimate: svy: logistic worry2 i.svy_year##i.citizen
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins 
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins pwcompare 

mi estimate: svy, over(svy_year): proportion worry2
mi estimate: svy: logistic worry2 i.svy_year##i.citizen
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins 
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins pwcompare
marginsplot, by(citizen)

 
/// DESCRIPTIVE STATISTICS BY DEPORTATION FEARS

mi estimate: svy: regress worry2
mi estimate: svy: regress worry2 i.citizen if citizen
mi estimate: svy: regress worry2 i.citizen if citizen==0
mi estimate: svy: regress age i.worry2 
mi estimate: svy: regress worry2 i.sex
mi estimate: svy: regress worry2 i.educ
mi estimate: svy: regress worry2 i.pparty
mi estimate: svy: regress hh_adults i.worry2
mi estimate: svy: regress worry2 i.census_region_res
mi estimate: svy: regress worry2 i.svy_year
mi estimate: svy: regress worry2 i.svy_year if svy_year==2007
mi estimate: svy: regress worry2 i.svy_year if svy_year==2008
mi estimate: svy: regress worry2 i.svy_year if svy_year==2010
mi estimate: svy: regress worry2 i.svy_year if svy_year==2013
mi estimate: svy: regress worry2 i.svy_year if svy_year==2016
mi estimate: svy: regress worry2 i.svy_year if svy_year==2018

*Figure 1
mi estimate, or: svy: logistic worry2 i.svy_year
mimrgns i.svy_year, predict(pr) cmdmargins vce(unconditional)
marginsplot


*Figure 2
mi estimate, or: svy: logistic worry2 i.svy_year##i.citizen
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins vce(unconditional)
marginsplot, by(citizen)
mimrgns i.svy_year#i.citizen if citizen, predict(pr) cmdmargins pwcompare


*Figure 3
mi estimate, or: svy: logistic worry2 i.citizen i.svy_year#i.citizen $controls
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins vce(unconditional)
marginsplot, by(citizen)
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins pwcompare


***Robustness Checks
*Measuring fear as a scale (Appendix Figure B1)
mi estimate, post: svy: ologit worry i.citizen#i.svy_year $controls
mimrgns citizen#svy_year, predict(default) cmdmargins vce(unconditional)
marginsplot, by(citizen)
mimrgns citizen#svy_year, predict(default) pwcompare

*Discrimination (Appendix Figure D1)
///only have discrimination in 2007, 2008, 2010, and 2018. 
mi estimate, or: svy: logistic worry2 citizen discrimination $controls i.svy_year##i.citizen 
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins
marginsplot, by(citizen)
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins pwcompare

*Family Heritage (Appendix Figure E1)
///have data in 2007, 2008, 2010, 2013, 2018
mi estimate, or: svy: logistic worry2 $controls i.svy_year##i.citizen i.heritage 
mimrgns i.svy_year#i.citizen, predict(pr) cmdmargins vce(unconditional)
marginsplot, by(citizen)

mi estimate, or: svy: logistic worry2 $controls i.svy_year##i.citizen##i.heritage 
mimrgns i.svy_year#i.citizen#i.heritage, predict(pr) cmdmargins vce(unconditional)
marginsplot, by(citizen)

*Presidental Admin Periodization *without svy_year controls and excluding single 
/// Trump time point (Figure 6)
global controls  age i.sex i.educ i.pparty hh_adults i.census_region_res
mi estimate, or: svy: logistic worry2 i.president##i.citizen $controls if president !=2
mimrgns i.president#i.citizen, predict(pr) cmdmargins vce(unconditional)
_marg_save, saving(f1, replace)

mi estimate, or: svy: logistic worry2 i.president##i.citizen $controls deportations_overall2 if president !=2
mimrgns i.president#i.citizen , predict(pr) cmdmargins vce(unconditional)
_marg_save, saving(f2, replace)

mi estimate, or: svy: logistic worry2 i.president##i.citizen $controls deportations_overall2 gtrends_deport if president !=2
mimrgns i.president#i.citizen, predict(pr) cmdmargins vce(unconditional) 
_marg_save, saving(f3, replace)
mimrgns i.president#i.citizen, predict(pr) cmdmargins pwcompare

combomarginsplot f1 f2 f3, by(citizen)

*Excluding 2016
mi estimate, or: svy: logistic worry2 i.president##i.citizen $controls deportations_overall2 gtrends_deport if president !=2 & svy_year !=2016
mimrgns i.president#i.citizen, predict(pr) cmdmargins vce(unconditional) 
mimrgns i.president#i.citizen, predict(pr) cmdmargins pwcompare

*Deportation Rates (Figure 4)
twoway scatter deportations_overall2 svy_year, sort xlabel(2007(1)2018)

*Google Trends (Figure 5)
twoway (scatter gtrends_deport svy_year, sort xlabel(2007(1)2018)) 

*Google Trends and News Mentions
twoway scatter news gtrends_deport svy_year, sort legend(pos(6)) xlabel(2007(1)2018)
corr news gtrends_deport

**Main Analysis Using Non-imputed Data & Listwise Deletion
use "pew_data_allyrs.dta", clear
global controls  age i.sex i.educ i.pparty hh_adults i.census_region_res i.svy_year
svyset id [pweight = svy_weight2] 

svy: logistic worry2 i.citizen $controls
svy: logistic worry2 i.citizen $controls i.svy_year#i.citizen
margins i.svy_year#i.citizen, predict(pr) vce(unconditional)

/////////Using all unique controls available in each survey years (Appendix Figure A)
svyset id [pweight = svy_weight2] 

gen filter_2007c = 0
replace filter_2007c = 1 if worry2 !=. & citizen!=. & age !=. & sex !=. & educ !=. & hhincome !=. & employment !=. & marital !=. & parent !=. & pparty !=. & yearslivedusa !=. & intvlanguage !=. & census_region_res !=. & hh_adults !=. & parentborn !=. & svy_year==2007
tab filter_2007c, m

svy: logistic worry2 citizen  age i.sex i.educ i.employment i.marital  i.parent ///
     i.hhincome i.pparty yearslivedusa   i.intvlanguage   i.census_region_res /// 
	  hh_adults  i.parentborn if svy_year==2007 & filter_2007c
estimates store d_nat_2007 

*2008 
gen filter_2008c = 0
replace filter_2008c = 1 if worry2 !=. & citizen!=. & age !=. & sex !=. & hhincome !=. & educ !=. & employment !=. & marital !=. & parent !=. & pparty & yearslivedusa !=. & intvlanguage !=. & intvrace !=. & census_region_res !=. & hh_adults !=. & parentborn !=. & svy_year==2008
tab filter_2008c, m

svy: logistic worry2 citizen age i.sex i.educ i.employment i.marital i.hhincome i.parent  ///
	  i.pparty yearslivedusa i.intvlanguage   i.census_region_res ///
	  hh_adults   i.parentborn  i.intvrace if svy_year==2008 
estimates store d_nat_2008

*2010
gen filter_2010c = 0
replace filter_2010c = 1 if worry2 !=. & citizen!=. & age !=. & sex !=. & hhincome !=. & educ !=. & employment !=. & marital !=. & parent !=. & pparty & yearslivedusa !=. & census_region_res !=. & hh_adults !=. & deported !=. & svy_year==2010 
tab filter_2010c, m

svy: logistic worry2 citizen  age i.sex i.educ i.employment i.marital i.hhincome i.parent  ///
	  i.pparty yearslivedusa deported   i.census_region_res ///
	  hh_adults   if svy_year==2010 
estimates store d_nat_2010

*2013 
gen filter_2013c = 0
replace filter_2013c = 1 if worry2 !=. & citizen!=. & age !=. & sex !=. & hhincome2 !=. & educ !=. & pparty !=. & yearslivedusa !=. & intvlanguage !=. & census_region_res !=. & hh_adults !=. & immsyst !=. & svy_year==2013
tab filter_2013c, m

svy: logistic worry2 citizen age i.sex i.hhincome2 i.educ    ///
		i.pparty yearslivedusa i.intvlanguage   i.census_region_res ///
		hh_adults   i.immsyst if svy_year==2013 & filter_2013c
estimates store d_nat_2013

*2016

gen filter_2016c = 0
replace filter_2016c = 1 if worry2 !=. & citizen!=. & age !=. & sex !=. & hhincome !=. & hhsize !=. & educ !=. & employment !=. & marital !=. & pparty !=. & census_region_res !=. & hh_adults !=. & svy_year==2016
tab filter_2016, m

svy: logistic worry2 citizen  age i.sex i.hhsize i.hhincome i.educ i.employment i.marital  ///
	  i.pparty  i.census_region_res  ///
	  hh_adults   if svy_year==2016 & filter_2016c
estimates store d_nat_2016


*2018 
gen filter_2018c = 0
replace filter_2018c = 1 if worry2 !=. & citizen!=. & age !=. & sex !=. & hhsize !=. & hhincome2 !=. & educ !=. & employment !=. & marital !=. & parent !=. & pparty !=. & census_region_res !=. & hh_adults !=. & svy_year==2018
tab filter_2018c, m

svy: logistic  worry2 citizen  age i.sex i.hhsize i.hhincome2 i.educ i.employment i.marital i.parent  ///
       i.pparty  yearslivedusa i.intvlanguage  i.census_region_res ///
	   hh_adults  if svy_year==2018 & filter_2018c
estimates store d_nat_2018

coefplot (d_nat_2007), bylabel(2007) xscale(r(-2,2))label(labsize(tiny)) ///
 || (d_nat_2008), bylabel(2008) ///
 || (d_nat_2010), bylabel(2010) ///
 || (d_nat_2013), bylabel(2013) ///
 || (d_nat_2016), bylabel(2016) ///
 || (d_nat_2018), bylabel(2018) ///
 || , drop(_cons) xline(0) byopts(compact rows(1) iscale(0.5))  
 



///////IMPUTATION USING LEGAL STATUS INDICATOR
use "pew_data_allyrs.dta", clear

*mi using mvn for legal_status indicator

mi set mlong
set seed 94305
mi register imputed worry2 legal_order age educ pparty registeredvoter hh_adults
mi register regular sex census_region_res svy_year
mi impute chained (logit) worry2 (mlogit) legal_order pparty registeredvoter (ologit) educ ///
(regress) age hh_adults = sex i.census_region_res i.svy_year, replace add(10) augment

*mi xeq 0: drop if citizens_missing==1
mi xeq 0: drop if legal_missing==1
mi xeq 0: drop if worry_missing==1

global controls  age i.sex i.educ i.pparty  hh_adults i.census_region_res i.svy_year
mi svyset id [pweight = svy_weight2] 

save "pew_data_allyrs-imputed-legal.dta", replace

*Figure 2
mi estimate, or: svy: logistic worry2 i.svy_year##i.legal_order
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins vce(unconditional)
marginsplot, by(legal_order)
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins pwcompare

*Figure 3
mi estimate, or: svy: logistic worry2 i.legal_order $controls i.svy_year#i.legal_order
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins vce(unconditional)
marginsplot, by(legal_order)
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins pwcompare

**Robustness Checks
*Fear measured as a scale (Appendix Figure B2)
mi estimate, post: svy: ologit worry i.legal_order#i.svy_year $controls
mimrgns legal_order#svy_year, predict(default) cmdmargins vce(unconditional)
marginsplot , by(legal_order)
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins pwcompare

*Discrimination (Appendix Figure D2)
///only have discrimination in 2007, 2008, 2010, and 2018. 
mi estimate, or: svy: logistic worry2 i.legal_order discrimination $controls i.svy_year##i.legal_order 
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins
marginsplot, by(legal_order)
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins pwcompare

*Family Heritage (Appendix Figure E2)
///have data in 2007, 2008, 2010, 2013, 2018
mi estimate, or: svy: logistic worry2 $controls i.svy_year##i.legal_order i.heritage 
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins
marginsplot, by(legal_order)
mimrgns i.svy_year#i.legal_order, predict(pr) cmdmargins pwcompare

*Presidental Admin Periodization *without svy_year controls and excluding single Trump
///data point
global controls  age i.sex i.educ i.pparty hh_adults i.census_region_res
mi estimate, or: svy: logistic worry2 i.president##i.legal_order $controls if president !=2
mimrgns i.president#i.legal_order, predict(pr) cmdmargins vce(unconditional)
_marg_save, saving(g1, replace)

mi estimate, or: svy: logistic worry2 i.president##i.legal_order $controls deportations_overall2 if president !=2
mimrgns i.president#i.legal_order , predict(pr) cmdmargins vce(unconditional)
_marg_save, saving(g2, replace)

mi estimate, or: svy: logistic worry2 i.president##i.legal_order $controls deportations_overall2 gtrends_deport if president !=2
mimrgns i.president#i.legal_order, predict(pr) cmdmargins vce(unconditional) 
_marg_save, saving(g3, replace)
mimrgns i.president#i.legal_order, predict(pr) cmdmargins pwcompare

combomarginsplot g1 g2 g3, by(legal_order)

*excluding 2016
global controls  age i.sex i.educ i.pparty hh_adults i.census_region_res
mi estimate, or: svy: logistic worry2 i.president##i.legal_order $controls deportations_overall2 gtrends_deport if president !=2 & svy_year !=2016
mimrgns i.president#i.legal_order, predict(pr) cmdmargins vce(unconditional) post
_marg_save, saving(b1, replace)
marginsplot, by(legal_order) 

**Main Analysis Using Non-imputed Data
use "pew_data_allyrs.dta", clear
global controls  age i.sex i.educ i.pparty hh_adults i.census_region_res i.svy_year
svyset id [pweight = svy_weight2] 

svy: logistic worry2 i.legal_order $controls
svy: logistic worry2 i.legal_order $controls i.svy_year#i.legal_order
